import { defineConfig } from 'vitepress'
import type { HeadConfig } from 'vitepress'
import type { PwaOptions } from '@vite-pwa/vitepress'
import { resolve } from 'node:path'
import { nav, sidebarGuide, sidebarComponents } from './web'
import { sidebarServer } from './server'
import { sidebarDeployer } from './deployer'
import mdItCustomAttrs from 'markdown-it-custom-attrs'
import { zhSearch } from './language'
// https://vitepress.dev/reference/site-config
export default defineConfig({
  title: 'WAdv Docs',
  description: 'WAdv快速上手指南',
  srcDir: 'src',
  base: '/docs/',
  head: head(),
  pwa: pwa(),
  markdown: {
    preConfig: (md) => {
      // use more markdown-it plugins!
      md.use(mdItCustomAttrs, 'image', {
        'data-fancybox': 'gallery'
      })
    }
  },
  ignoreDeadLinks: true,
  themeConfig: {
    logo: '/cat.svg',
    // https://vitepress.dev/reference/default-theme-config
    nav: nav(),
    search: {
      provider: 'local',
      options: {
        detailedView: true,
        locales: {
          ...zhSearch
        }
      }
    },
    sidebar: {
      '/guide/': { base: '/guide/', items: sidebarGuide() },
      '/components/': { base: '/components/', items: sidebarComponents() },
      '/server/': { base: '/server/', items: sidebarServer() },
      '/deployer/': { base: '/deployer/', items: sidebarDeployer() }
    },
    // [
    //   {
    //     text: '指南',
    //     items: [
    //       { text: 'Markdown Examples', link: '/markdown-examples' },
    //       { text: 'Runtime API Examples', link: '/api-examples' }
    //     ]
    //   }
    // ],
    socialLinks: [{ icon: 'github', link: 'https://gitee.com/wangdachuia/vue-wadv-admin.git' }],

    darkModeSwitchLabel: '主题',
    darkModeSwitchTitle: '切换到深色模式',
    docFooter: {
      next: '下一页',
      prev: '上一页'
    },
    outline: {
      label: '页面导航'
    },
    returnToTopLabel: '回到顶部',
    sidebarMenuLabel: '菜单'
  },
  vite: {
    build: {
      chunkSizeWarningLimit: Infinity,
      minify: 'terser'
    },
    json: {
      stringify: true
    },
    server: {
      fs: {
        allow: ['../..']
      },
      host: true,
      port: 6173
    },
    ssr: {
      external: ['@vue/repl']
    }
  }
})

function head(): HeadConfig[] {
  return [
    // jsdelivr
    // [
    //   'link',
    //   { rel: 'stylesheet', href: 'https://cdn.jsdelivr.net/npm/@fancyapps/ui/dist/fancybox.css' }
    // ],
    // ['script', { src: 'https://cdn.jsdelivr.net/npm/@fancyapps/ui@4.0/dist/fancybox.umd.js' }],
    
    // unpkg
    [
      'link',
      { rel: 'stylesheet', href: 'https://unpkg.com/@fancyapps/ui@4.0.31/dist/fancybox.css' }
    ],
    ['script', { src: 'https://unpkg.com/@fancyapps/ui@4.0.31/dist/fancybox.umd.js' }],
    ['link', { href: '/docs/favicon.ico', rel: 'icon' }]
  ]
}

function pwa(): PwaOptions {
  return {
    includeManifestIcons: false,
    manifest: {
      description: 'WAdv Admin is a modern admin dashboard template based on Vue 3. ',
      id: '/',
      name: 'WAdv Admin Doc',
      short_name: 'wadv_admin_doc',
      theme_color: '#ffffff'
    },
    outDir: resolve(process.cwd(), '.vitepress/dist'),
    registerType: 'autoUpdate',
    workbox: {
      globPatterns: ['**/*.{css,js,html,svg,png,ico,txt,woff2}']
    }
  }
}
